<?php
$error = "";
$msg = "";
$tmp = $_FILES['fileToUpload']['tmp_name'];
$nom = $_FILES['fileToUpload']['name'];
$repertoireDestination = "../images/";


if (! empty($_FILES['fileToUpload']['error']))
{
    switch($_FILES['fileToUpload']['error'])
    {

        case '1':
            $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
            break;
        case '2':
            $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
            break;
        case '3':
            $error = 'The uploaded file was only partially uploaded';
            break;
        case '4':
            $error = 'No file was uploaded.';
            break;

        case '6':
            $error = 'Missing a temporary folder';
            break;
        case '7':
            $error = 'Failed to write file to disk';
            break;
        case '8':
            $error = 'File upload stopped by extension';
            break;
        case '999':
        default:
            $error = 'No error code avaiable';
    }
} elseif ( empty($tmp) || $tmp == 'none')
{
    $error = 'No file was uploaded..';
} else
{
// Calcul des nouvelles dimensions
    list ($width, $height) = getimagesize($tmp);
	
    if($width >= $height)
	{
		$new_width = 512;
		$new_height = $height*512/$width;
	}
	else{
		$new_width = $width*512/$height;
		$new_height = 512;
	}

    
    // Redimensionnement
    $image_new = @imagecreatetruecolor($new_width, $new_height)
    or die ("Cannot Initialize new GD image stream");


    $blanc = ImageColorAllocate($image_new, 255, 255, 255);
    $extention = explode('.', $nom);
    $extention = strtolower($extention[count($extention)-1]);

    /*if ($extention == 'jpeg' || $extention == 'jpg')
     {*/
    $image = imagecreatefromjpeg($tmp);
    imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    // enregistrement
    imagejpeg($image_new, $repertoireDestination.$nom);
	imagedestroy($image_new);
	
    /*if (is_uploaded_file($tmp))
    {
        //on déplace le fichier dans le répertoire final

        if (move_uploaded_file($tmp, $repertoireDestination.$nom))
        {

        }
		else{
			$error = 'upload fail';
		}

    }*/



    $repertoirePreview = "../imagespreview/";
    // Calcul des nouvelles dimensions
    list ($width, $height) = getimagesize($repertoireDestination.$nom);
    $new_width = ($width >= $height)?60:60/$width;
    $new_height = ($height >= width)?60:60/$height;
    // Redimensionnement
    $image_new = @imagecreatetruecolor($new_width, $new_height)
    or die ("Cannot Initialize new GD image stream");


    $blanc = ImageColorAllocate($image_new, 255, 255, 255);
    $extention = explode('.', $nom);
    $extention = strtolower($extention[count($extention)-1]);

    /*if ($extention == 'jpeg' || $extention == 'jpg')
     {*/
    $image = imagecreatefromjpeg($repertoireDestination.$nom);
    imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    // enregistrement
    imagejpeg($image_new, $repertoirePreview.$nom);
    /*  }
     else if ($extention == 'png')
     {
     $image = imagecreatefrompng($repertoireDestination.$nom);
     imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
     imagepng($image_new, $repertoirePreview.$nom);
     }
     elseif ($extention == 'gif')
     {
     $image = imagecreatefromgif($repertoireDestination.$nom);
     imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
     imagegif($image_new, $repertoirePreview.$nom);
     }
     else
     {
     echo "Format incorrect.";
     }*/
    imagedestroy($image_new);


include_once "../php/ini.php";
    dbconnect();

    $sql = "INSERT INTO `images` (`id_image` , `id_user` , `nom_image` , `coord_x` , `coord_y` , `coord_z` , `droit`, `posted` ) VALUES ( NULL , '', '".$nom."', '', '', '', 'private', 'non' );";
    $sqlresults = mysql_query($sql);
    if (!$sqlresults)
    {
        echo '{"status":"ERROR: '.$sql.'"}';
    }
    else
    {
        $sql2 = "SELECT * FROM `images` ORDER BY id_image DESC LIMIT 1;";
        $result = mysql_query($sql2);
        $tab = mysql_fetch_assoc($result);
        echo "{";
        echo "error: '".$error."',\n";
        echo "nom: '".$nom."',\n";
		echo "id: '".$tab['id_image']."'\n";
        echo "}";

    }


    dbclose();


}

?>
